﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using OA.DBUtility;
using OA.Model;

namespace OA.Manager
{
    public class ReimbursementManager
    {
        private SqlService<ReimbursementInfo> _reimbursementService = null;
        private SqlService<ReimbursementPlaneTicketInfo> _reimbursementPlaneTicketService = null;
        private SqlService<ReimbursementItemInfo> _reimbursementItemService = null;
        private SqlService<ReimbursementDictionaryInfo> _reimbursementDictionaryService = null;
        private SqlService<ReimbursementBusinessTravelInfo> _reimbursementTravelService = null;

        public ReimbursementManager()
        {
            _reimbursementDictionaryService = new SqlService<ReimbursementDictionaryInfo>();
            _reimbursementItemService = new SqlService<ReimbursementItemInfo>();
            _reimbursementPlaneTicketService = new SqlService<ReimbursementPlaneTicketInfo>();
            _reimbursementService = new SqlService<ReimbursementInfo>();
            _reimbursementTravelService = new SqlService<ReimbursementBusinessTravelInfo>();
        }


        /// <summary>
        /// 插入报销记录，成功后返回刚插入数据生成的自增ID
        /// </summary>
        /// <param name="recruitmentInfo"></param>
        /// <returns></returns>
        public int InsertReimbursementInfo(ReimbursementInfo recruitmentInfo)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@Id", SqlDbType.Int, 4),
                                            new SqlParameter("@ProjectNum", SqlDbType.VarChar, 50),
                                            new SqlParameter("@EmployeeId", SqlDbType.Int, 4),
                                            new SqlParameter("@Description", SqlDbType.NVarChar, 200),
                                            new SqlParameter("@CostType", SqlDbType.NVarChar, 50),
                                            new SqlParameter("@BillCount", SqlDbType.Int, 4),
                                            new SqlParameter("@TotalMoney", SqlDbType.Money, 8),
                                            new SqlParameter("@ReimbursementTime", SqlDbType.DateTime)
                                        };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = recruitmentInfo.ProjectNum;
            parameters[2].Value = recruitmentInfo.EmployeeId;
            parameters[3].Value = recruitmentInfo.Description;
            parameters[4].Value = recruitmentInfo.CostType;
            parameters[5].Value = recruitmentInfo.BillCount;
            parameters[6].Value = recruitmentInfo.TotalMoney;
            parameters[7].Value = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            int id;
            if (_reimbursementItemService.ExecuteNonQueryByProcedure("Proc_Reimbursement", parameters) > 0)
                int.TryParse(parameters[0].Value.ToString(), out id);
            else
                id = 0;
            return id;
        }

        /// <summary>
        /// 增加报销详情信息
        /// </summary>
        /// <param name="reimbursementItem"></param>
        /// <returns></returns>
        public bool InsertReimbursementItemInfo(ReimbursementItemInfo reimbursementItem)
        {
            return _reimbursementItemService.SaveEntity(reimbursementItem);
        }

        /// <summary>
        /// 增加出差补助信息
        /// </summary>
        /// <param name="reimbursementBusinessTrip"></param>
        /// <returns></returns>
        public bool InsertReimbursementBusinessTripInfo(ReimbursementBusinessTravelInfo reimbursementBusinessTrip)
        {
            return _reimbursementTravelService.SaveEntity(reimbursementBusinessTrip);
        }

        /// <summary>
        /// 修改出差补助信息
        /// </summary>
        /// <param name="reimbursementBusinessTrip"></param>
        /// <param name="needUpdateFields"></param>
        /// <returns></returns>
        public bool UpdateReimbursementBusinessTripInfo(ReimbursementBusinessTravelInfo reimbursementBusinessTrip,
                                                        List<string> needUpdateFields)
        {
            return _reimbursementTravelService.UpdateByFields(reimbursementBusinessTrip, needUpdateFields);
        }

        /// <summary>
        /// 增加机票报销信息
        /// </summary>
        /// <param name="reimbursementPlaneTicket"></param>
        /// <returns></returns>
        public bool InsertReimbursementPlaneTicketInfo(ReimbursementPlaneTicketInfo reimbursementPlaneTicket)
        {
            return _reimbursementPlaneTicketService.SaveEntity(reimbursementPlaneTicket);
        }

        /// <summary>
        /// 修改机票报销信息
        /// </summary>
        /// <param name="reimbursementPlaneTicket"></param>
        /// <param name="needUpdateFields"></param>
        /// <returns></returns>
        public bool UpdateReimbursementPlaneTicketInfo(ReimbursementPlaneTicketInfo reimbursementPlaneTicket,
                                                       List<string> needUpdateFields)
        {
            return _reimbursementPlaneTicketService.UpdateByFields(reimbursementPlaneTicket, needUpdateFields);
        }

        /// <summary>
        /// 根据Id获得保险内容信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ReimbursementInfo GetReimbursementInfoByID(int ID)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Id", ID);
            return _reimbursementService.GetSingleByFields(dic, null);
        }
        public ReimbursementInfoView GetReimbursementInfoViewByID(int ID)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Id", ID);
            return new SqlService<ReimbursementInfoView>().GetSingleByFields(dic, null);
        }

        /// <summary>
        /// 根据报销ID查询报销详情信息
        /// </summary>
        /// <param name="reimbursementId"></param>
        /// <returns></returns>
        public ReimbursementItemInfo GetReimbursementItemInfoByReimbursementId(int reimbursementId)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("ReimbursementId", reimbursementId);
            return _reimbursementItemService.GetSingleByFields(dic, null);
        }

        /// <summary>
        /// 判断该报销信息是否存在
        /// </summary>
        /// <param name="recruitmentInfo"></param>
        /// <returns></returns>
        public bool IsExistReimbursementInfo(ReimbursementInfo recruitmentInfo)
        {
            return _reimbursementService.IsExist(recruitmentInfo);
        }

        /// <summary>
        /// 判断报销机票信息是否存在
        /// </summary>
        /// <param name="reimbursementPlaneTicket"> </param>
        /// <returns></returns>
        public bool IsExistReimbursementInfo(ReimbursementPlaneTicketInfo reimbursementPlaneTicket)
        {
            return _reimbursementPlaneTicketService.IsExist(reimbursementPlaneTicket);
        }

        /// <summary>
        /// 判断该出差信息是否存在
        /// </summary>
        /// <param name="reimbursementBusinessTravel"> </param>
        /// <returns></returns>
        public bool IsExistReimbursementInfo(ReimbursementBusinessTravelInfo reimbursementBusinessTravel)
        {
            return _reimbursementTravelService.IsExist(reimbursementBusinessTravel);
        }

        /// <summary>
        /// 根据指定的字段更新，字段名是数据库中对应的名称
        /// </summary>
        /// <param name="recruitmentInfo"></param>
        /// <param name="needUpdateFields"></param>
        /// <returns></returns>
        public bool UpdateReimbursementInfo(ReimbursementInfo recruitmentInfo, List<string> needUpdateFields)
        {
            return _reimbursementService.UpdateByFields(recruitmentInfo, needUpdateFields, "Reimbursement");
        }

        /// <summary>
        /// 更新报销详情信息
        /// </summary>
        /// <param name="reimbursementItem"></param>
        /// <param name="needUpdateFields"></param>
        /// <returns></returns>
        public bool UpdateReimbursementItemInfo(ReimbursementItemInfo reimbursementItem, List<string> needUpdateFields)
        {
            return _reimbursementItemService.UpdateByFieldsFilter(reimbursementItem, needUpdateFields,
                                                                  " CostItemNo='" + reimbursementItem.CostItemNo +
                                                                  "' and ReimbursementId='" +
                                                                  reimbursementItem.ReimbursementId.ToString(
                                                                      CultureInfo.InvariantCulture) + "'");
        }

        /// <summary>
        /// 删除该检查任务
        /// </summary>
        /// <param name="recruitmentInfo"></param>
        /// <returns></returns>
        public bool DeleteReimbursementInfo(ReimbursementInfo recruitmentInfo)
        {
            return _reimbursementService.DeleteEntity(recruitmentInfo);
        }
        public bool DeleteReimbursementPlaneTicketInfo(ReimbursementPlaneTicketInfo recruitmentInfo)
        {
            return _reimbursementPlaneTicketService.DeleteEntity("ReimbursementPlaneTicket", " ReimbursementId=" + recruitmentInfo.ReimbursementId);
        }

        public bool DeleteReimbursementItemInfo(ReimbursementItemInfo recruitmentInfo)
        {
            return _reimbursementService.DeleteEntity("ReimbursementItem", " ReimbursementId=" + recruitmentInfo.ReimbursementId);
        }
        public bool DeleteReimbursementBusinessTravelInfo(ReimbursementBusinessTravelInfo recruitmentInfo)
        {
            return _reimbursementTravelService.DeleteEntity("ReimbursementBusinessTravel", " ReimbursementId=" + recruitmentInfo.ReimbursementId);
        }

        public DataTable ReimbursementPaged(ReimbursementInfo info, int pageIndex, int pageSize)
        {
            int total = 0;
            DataTable dt = _reimbursementService.GetSearchPaged(info, pageIndex, pageSize, ref total);
            return dt;
        }
        public DataTable ReimbursementPagedView(ReimbursementInfoView info, int pageIndex, int pageSize)
        {
            int total = 0;
            DataTable dt = new SqlService<ReimbursementInfoView>().GetSearchPaged(info, pageIndex, pageSize, ref total);
            return dt;
        }

        /// <summary>
        /// 根据自定义条件取得机票信息
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ReimbursementPlaneTicketInfo GetReimbursementPlaneTicketInfo(string filter)
        {
            string sql = "SELECT * FROM dbo.ReimbursementPlaneTicket WHERE 1=1 AND " + filter;
            return _reimbursementPlaneTicketService.GetSingleBySqlString(sql);
        }

        /// <summary>
        /// 根据自定义条件取得出差信息
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ReimbursementBusinessTravelInfo GetReimbursementBusinessTravelInfo(string filter)
        {
            string sql = "SELECT * FROM dbo.ReimbursementBusinessTravel WHERE 1=1 AND " + filter;
            return _reimbursementTravelService.GetSingleBySqlString(sql);
        }
    }
}
